package me.zhengjie.modules.common.rest;

import java.sql.Timestamp;
import java.util.List;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.common.domain.TRegion;
import me.zhengjie.modules.common.service.dto.TRegionDTO;
import me.zhengjie.modules.common.service.dto.TRegionQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import me.zhengjie.modules.common.service.TRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by bruce on 2021/4/23.
 */
@RestController
@RequestMapping("api")
public class TRegionController {
    @Autowired
    private TRegionService tRegionService;


    @Log("查询TRegion")
    @GetMapping(value = "/tRegion")
    public ResponseEntity getTRegions(TRegionQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tRegionService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @Log("增加TRegion")
    @PostMapping(value = "/tRegion")
    public ResponseEntity create(@Validated @RequestBody TRegion region){
        TRegion r = tRegionService.findByName(region.getName());
        if(r != null) {
            return HttpResponseUtils.passDefaultResponse("地域名称已存在", HttpStatus.BAD_REQUEST);
        }

        if(region.getPid() == null) {
            region.setPid(0L);
            region.setLevel(0);
        } else {
            TRegionDTO pe = tRegionService.findById(region.getPid());
            if(pe == null) {
                return HttpResponseUtils.passDefaultResponse("地域信息不存在", HttpStatus.BAD_REQUEST);
            }
            region.setLevel(pe.getLevel() + 1);
        }

        region.setGmtCreate(new Timestamp(System.currentTimeMillis()));
        return new ResponseEntity(tRegionService.create(region),HttpStatus.OK);
    }

    @Log("修改TRegion")
    @PutMapping(value = "/tRegion")
    public ResponseEntity update(@Validated @RequestBody TRegion region){
        TRegionDTO r = tRegionService.findById(region.getId());
        if(r == null) {
            return HttpResponseUtils.passDefaultResponse("地域信息不存在", HttpStatus.BAD_REQUEST);
        } else {
            TRegionQueryCriteria criteria = new TRegionQueryCriteria();
            criteria.setName(region.getName());
            List<TRegion> list = (List<TRegion>)tRegionService.queryAll(criteria);
            if(list.size() > 1) {
                return HttpResponseUtils.passDefaultResponse("地域名称冲突", HttpStatus.BAD_REQUEST);
            }
            if(region.getPid() == null) {
                region.setPid(0L);
                region.setLevel(0);
            } else {
                TRegionDTO pe = tRegionService.findById(region.getPid());
                if(pe == null) {
                    return HttpResponseUtils.passDefaultResponse("地域信息不存在", HttpStatus.BAD_REQUEST);
                }
                region.setLevel(pe.getLevel() + 1);
            }

            region.setGmtModified(new Timestamp(System.currentTimeMillis()));
            tRegionService.update(region);
            return new ResponseEntity(HttpStatus.OK);
        }
    }

    @Log("删除tRegion")
    @DeleteMapping(value = "/tRegion/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        TRegionDTO r = tRegionService.findById(id);
        if(r == null) {
            return HttpResponseUtils.passDefaultResponse("地域信息不村子", HttpStatus.BAD_REQUEST);
        }
        TRegionQueryCriteria criteria = new TRegionQueryCriteria();
        criteria.setPid(id); //
        List<TRegion> regions = (List<TRegion>)tRegionService.queryAll(criteria);
        if(regions.size() > 0) {
            return HttpResponseUtils.passDefaultResponse("当前区域存在子区域,不能删除", HttpStatus.BAD_REQUEST);
        }

        tRegionService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
